<template>
    <div class="contaner">
    <div class="header">
        <div class="head">
            <div class="titleName">{{ goodsInfo.goods_name }}</div>
            <div class="titleList">
                <div class="listItem" v-for="(item,index) in titleList" :key="index">{{ item }}</div>
            </div>
        </div>
    </div>
    <div class="content">
      <div class="block">
        <el-carousel :style="{ width: screenWidth, height: screenWidth }" height="586px">
          <el-carousel-item v-for="(item,index) in goods_imgs" :key="index">
            <img style="width:100%" :src="item.img" alt="" lang="en">
          </el-carousel-item>
        </el-carousel>
      </div>
      <div
        class="store_info"
        :style="{ width: screenWidth, height: screenWidth }"
      >
      <div class="info_top">
        <div class="info_top_name">{{ goodsInfo.goods_name }}</div>
        <div class="info_main">
          {{ goodsInfo.goods_infos }}
        </div>
        <div class="store_name">{{ goodsInfo.goods_store }}</div>
        <div class="info_top_price">
          <div class="info_top_pprice">{{goodsInfo.goods_pprice}}元</div>
          <div class="info_top_oprice">{{goodsInfo.goods_oprice}}元</div>
        </div>
      </div>
      <div class="info_botm">
        <div class="info_botm_box">
          <div class="info_botm_box_name_price">
            <div class="info_botm_box_name">
              {{ goodsInfo.goods_name }}
            </div>
            <div class="info_botm_box_price">
              <div class="info_botm_box_pprice">
                {{goodsInfo.goods_pprice}}元
              </div>
              <div class="info_botm_box_oprice">
                {{goodsInfo.goods_oprice}}元
              </div>
            </div>
          </div>
          <div class="total">
            总计 : {{goodsInfo.goods_pprice}}元
          </div>
        </div>
        <div class="info_botm_btn">
          <div class="info_botm_btn_item">
            <div class="add_cart btn" @click="addCart">
              加入购物车
            </div>
            <div class="collect btn" @click="collectCart">
              喜欢
            </div>
          </div>
          <div class="pro-policy">
            <div class="policy_item" v-for="(policyItem,index) in policy" :key="index">
              <img class="icon-img" src="../static/imgs/成功.png" alt="">
              {{ policyItem }}
            </div>
          </div>
        </div>
      </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  data() {
    return {
      screenWidth: (document.body.clientWidth * 0.8) / 2 + "px",
      goodsInfo: {},
      goods_imgs:[],
      titleList:['概数','参数','评价'],
      policy:['小米自营','小米发货','7天无理由退货','7天价格保护'],
      productID:'',//当前商品id
    };
  },
  computed: {
    ...mapState({
      token:state=>state.user.token
    })
  },
  created() {
    let productID = sessionStorage.getItem("productID");
    this.productID = productID
    this.getDetail(productID);
  },
  methods: {
    getDetail(ID) {
      this.$http
        .get("/goods/singleGoods", {
          params: {
            productID: ID,
          },
        })
        .then((res) => {
          this.goodsInfo = res.data.result;
          const imgs = res.data.result.goods_imgs
          this.goods_imgs = JSON.parse(imgs)
        });
    },
    async addCart(){
      const res = await this.$http.post('/carts/addCart',{goods_id:this.productID,goods_num:2})
      console.log(res);
      if(res.status == 200){
        this.$message({
          message:res.data.message,
          type:'success'
        })
      }
    },
    async collectCart(){
      const res = await this.$http.post('/goods/collect',{goods_id:this.productID})
      if(res.data.code == 200){
        this.$message({
          message:res.data.message,
          type:'success'
        })
      }else if(res.data.code == 201){
        this.$message({
          message:res.data.message,
          type:'warning'
        })
      }
    }
  },
};
</script>

<style scoped>
.contaner{
  margin-bottom: 20px;
}
.Detail{
    display: flex;
    justify-content: center;
    align-items: center;
}
.carousel-item{
  max-width: 100%;
  max-height: 100%;
  background-color: red;
}
.header{
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #ffffff;
    box-shadow: 0px 4px 8px #dfdddd;;
}
.head{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60px;
    width: 80%;
}
.titleList{
    display: flex;
}
.listItem{
    padding: 0 8px 0 8px;
    cursor: pointer;
}
.listItem:hover{
    color: #ff6700;
}
.content {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 8px;
}
.banner {
  /* width: 650px;
    height: 650px; */
  border: 1px solid #ccc;
}
.img {
  width: 100%;
}
.store_info {
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
}
.info_top{
  padding: 26px 0 20px 10px;
  border-bottom: 1px solid #ccc;
}
.info_top_name{
  font-size: 22px;
  margin-top: 10px;
}
.info_main{
  font-size: 14px;
  color: #ccc;
  margin-top: 16px;
}
.store_name{
  margin-top: 10px;
  color: #ff6700;
}
.info_top_price{
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 28%;
  margin-top: 20px;
}
.info_top_pprice {
  font-size: 18px;
  color: #ff6700;
}
.info_top_oprice {
  color: #ccc;
  text-decoration: line-through;
}
.info_botm{
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  flex-grow: 1;
}
.info_botm_box{
  width: 90%;
  height: 80px;
  margin-top: 16px;
  background-color: #f9f9fa;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  padding: 30px;
}
.info_botm_btn {
  display: flex;
  flex-direction: column;
  margin-top: 26px;
}
.info_botm_btn_item{
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.add_cart{
  background-color: #ff6700;
  
}
.add_cart:hover{
  background-color: #f25807;
}
.collect{
  background-color: #b0b0b0;
}
.collect:hover{
  background-color: #757575;
}
.btn {
  display: flex;
  justify-content: center;
  align-items: center;
  color: #ffffff;
  border-radius: 4px;
  width: 46%;
  height: 60px;
  cursor: pointer;
}
.pro-policy{
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.policy_item{
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  margin-right: 20px;
  color: #b0b0b0;
}
.icon-img{
  width: 20px;
}
.info_botm_box_name_price{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.info_botm_box_price{
  width: 28%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 0 50px 0 50px;
}
.info_botm_box_oprice{
  text-decoration: line-through;
}
.total{
  font-size: 24px;
  color: #f25807;
}
</style>